rm(list=ls(all=TRUE))

path <- "" #here you need to adjust the path macro to the place on your computer
setwd(path)

library(haven)
library(dplyr)
library(ggplot2)
library(Hmisc)
library(ggrepel)

dat <- read_dta(paste0(path, "\\party_governments.dta"))

##################################
###### Figure 5, appendix C ######
##################################

dat$iso2code <- as_factor(dat$iso2code)
dat$country <- as_factor(dat$country)

# adjusting the sample, dropping parties which have not sponsored 
# protests and cabinets under which no protest has happened.

table(dat$country)
length(unique(dat$country))


# adding some new variables

dat$in_parl[dat$seat_sh_bef<=0] <- 0
dat$in_parl[dat$seat_sh_bef>0] <- 1
table(dat$in_parl)

dat <- dat %>% 
  group_by(iso2code, cabinet_name) %>% 
  mutate(largest= ifelse(vote_bef==max(vote_bef), 1, 0))

dim(plyr::count(dat[dat$largest==1,], vars = c("iso2code", "cabinet_name")))
dim(plyr::count(dat, vars = c("iso2code", "cabinet_name")))

dat$large[dat$vote_bef>=25] <- 1
dat$large[dat$vote_bef<25] <- 0

dat$mid_size[dat$vote_bef>=10 & dat$vote_bef<25] <- 1
dat$mid_size[dat$vote_bef<10 | dat$vote_bef>=25] <- 0

dat$small[dat$vote_bef<10] <- 1
dat$small[dat$vote_bef>10] <- 0

summary(dat$left_right)
summary(dat$cab_leftright)
summary(dat$state_market)
summary(dat$liberty_authority)

dat$left_prt[dat$left_right<5] <- 1
dat$left_prt[dat$left_right>=5] <- 0

dat$left_cab[dat$cab_leftright<5] <- 1
dat$left_cab[dat$cab_leftright>=5] <- 0

dat$eleft_prt[dat$state_market<5] <- 1
dat$eleft_prt[dat$state_market>=5] <- 0

dat$cleft_prt[dat$liberty_authority<5] <- 1
dat$cleft_prt[dat$liberty_authority>=5] <- 0

dat$two_gov[dat$gov_counter>=2] <- 1
dat$two_gov[dat$gov_counter<2] <- 0

dat$one_gov[dat$gov_counter>=1] <- 1
dat$one_gov[dat$gov_counter<1] <- 0

dat$past_pm[dat$pm_counter>=1] <- 1
dat$past_pm[dat$pm_counter<1 | is.na(dat$pm_counter)] <- 0

dat$mainstream[dat$family2==2 | dat$family2==4 | dat$family2==6 | dat$family2==8] <- 1
dat$mainstream[is.na(dat$mainstream) & !is.na(dat$family2)] <- 0
table(dat$mainstream)

cab_cntr <-  dat %>% 
  group_by(iso2code, cabinet_name) %>%
  summarise_at(vars(bmr_demdur, vdem_civil_soc,
                    all_evts, all_parties_evts), funs(mean(., na.rm=TRUE)))

cab_cntr <- within(cab_cntr, prt_share <- all_parties_evts*100/all_evts)
table(cab_cntr$prt_share)
cab_cntr$cabinet_name[cab_cntr$prt_share>100]
quantile(cab_cntr$prt_share, na.rm=TRUE)[3]
mean(cab_cntr$prt_share, na.rm=TRUE)

cab_cntr$ids <- paste0(cab_cntr$cabinet_name, ' (', cab_cntr$iso2code, ')')


# low and high contexts

cab_cntr$civil_soc_cat <- cut2(cab_cntr$vdem_civil_soc, g=2)
table(cab_cntr$civil_soc_cat, cab_cntr$iso2code)

# differentiated countries

nodiff.cntry <- c("BG", "CY", "CZ", "EE", "DE", "HU", "IS", "LV", "LT", "SK", "CH")
diff.cntry <- c("AT", "BE", "DK", "FI", "FR", "GR", "IE", "IT", "LU", "MT", "NL", "NO", "PL", "PT", "RO", "SI", "ES", "SE", "UK")

cab_cntr$prt_share_cat[as.character(cab_cntr$iso2code) %in% diff.cntry] <- 0
cab_cntr$prt_share_cat[as.character(cab_cntr$iso2code) %in% nodiff.cntry] <- 1

cab_cntr$prt_share_cat <- relevel(as.factor(cab_cntr$prt_share_cat), "0")

dat_upper_lev_cat <- subset(cab_cntr, select=c("iso2code",
                                               "cabinet_name",
                                               "civil_soc_cat",
                                               "prt_share",
                                               "prt_share_cat"))
dat <- merge(dat, dat_upper_lev_cat)
rm(dat_upper_lev_cat, cab_cntr)


# tables with shares


#mainstream

hdat1.1 <- dat %>%
  filter(!is.na(civil_soc_cat) & !is.na(mainstream)) %>% 
  group_by(civil_soc_cat, mainstream) %>% 
  summarise_at(vars(parties_evts), funs(sum(., na.rm=TRUE)))
hdat1.2 <- dat %>%
  filter(!is.na(civil_soc_cat) & !is.na(mainstream)) %>% 
  group_by(civil_soc_cat) %>% 
  summarise(group_tot=sum(parties_evts, na.rm=TRUE))
hdat1 <- merge(hdat1.1, hdat1.2)
hdat1 <- hdat1 %>% 
  mutate(sh= case_when(mainstream==1 ~ parties_evts*100/group_tot)) %>% 
  select(civil_soc_cat, sh) %>% 
  filter(complete.cases(.))
hdat1 <- t(hdat1[,2])
colnames(hdat1) <- c("low civ", "high civ")
print(hdat1)
hdat2.1 <- dat %>%
  filter(!is.na(region) & !is.na(mainstream)) %>% 
  group_by(region, mainstream) %>% 
  summarise_at(vars(parties_evts), funs(sum(., na.rm=TRUE)))
hdat2.2 <- dat %>%
  filter(!is.na(region) & !is.na(mainstream)) %>% 
  group_by(region) %>% 
  summarise(group_tot=sum(parties_evts, na.rm=TRUE))
hdat2 <- merge(hdat2.1, hdat2.2)
hdat2 <- hdat2 %>% 
  mutate(sh= case_when(mainstream==1 ~ parties_evts*100/group_tot)) %>% 
  select(region, sh) %>% 
  filter(complete.cases(.))
hdat2 <- t(hdat2[,2])
colnames(hdat2) <- c("NWE", "SE", "EE")
print(hdat2)
hdat3.1 <- dat %>%
  filter(!is.na(prt_share_cat) & !is.na(mainstream)) %>% 
  group_by(prt_share_cat, mainstream) %>% 
  summarise_at(vars(parties_evts), funs(sum(., na.rm=TRUE)))
hdat3.2 <- dat %>%
  filter(!is.na(prt_share_cat) & !is.na(mainstream)) %>% 
  group_by(prt_share_cat) %>% 
  summarise(group_tot=sum(parties_evts, na.rm=TRUE))
hdat3 <- merge(hdat3.1, hdat3.2)
hdat3 <- hdat3 %>% 
  mutate(sh= case_when(mainstream==1 ~ parties_evts*100/group_tot)) %>% 
  select(prt_share_cat, sh) %>% 
  filter(complete.cases(.))
hdat3 <- t(hdat3[,2])
colnames(hdat3) <- c("low pp", "high pp")
print(hdat3)
table <- cbind("mainstream", hdat1, hdat2, hdat3)
rm(list=ls(pattern="hdat"))


#past_pm

hdat1.1 <- dat %>%
  filter(!is.na(civil_soc_cat) & !is.na(past_pm)) %>% 
  group_by(civil_soc_cat, past_pm) %>% 
  summarise_at(vars(parties_evts), funs(sum(., na.rm=TRUE)))
hdat1.2 <- dat %>%
  filter(!is.na(civil_soc_cat) & !is.na(past_pm)) %>% 
  group_by(civil_soc_cat) %>% 
  summarise(group_tot=sum(parties_evts, na.rm=TRUE))
hdat1 <- merge(hdat1.1, hdat1.2)
hdat1 <- hdat1 %>% 
  mutate(sh= case_when(past_pm==1 ~ parties_evts*100/group_tot)) %>% 
  select(civil_soc_cat, sh) %>% 
  filter(complete.cases(.))
hdat1 <- t(hdat1[,2])
colnames(hdat1) <- c("low civ", "high civ")
print(hdat1)
hdat2.1 <- dat %>%
  filter(!is.na(region) & !is.na(past_pm)) %>% 
  group_by(region, past_pm) %>% 
  summarise_at(vars(parties_evts), funs(sum(., na.rm=TRUE)))
hdat2.2 <- dat %>%
  filter(!is.na(region) & !is.na(past_pm)) %>% 
  group_by(region) %>% 
  summarise(group_tot=sum(parties_evts, na.rm=TRUE))
hdat2 <- merge(hdat2.1, hdat2.2)
hdat2 <- hdat2 %>% 
  mutate(sh= case_when(past_pm==1 ~ parties_evts*100/group_tot)) %>% 
  select(region, sh) %>% 
  filter(complete.cases(.))
hdat2 <- t(hdat2[,2])
colnames(hdat2) <- c("NWE", "SE", "EE")
print(hdat2)
hdat3.1 <- dat %>%
  filter(!is.na(prt_share_cat) & !is.na(past_pm)) %>% 
  group_by(prt_share_cat, past_pm) %>% 
  summarise_at(vars(parties_evts), funs(sum(., na.rm=TRUE)))
hdat3.2 <- dat %>%
  filter(!is.na(prt_share_cat) & !is.na(past_pm)) %>% 
  group_by(prt_share_cat) %>% 
  summarise(group_tot=sum(parties_evts, na.rm=TRUE))
hdat3 <- merge(hdat3.1, hdat3.2)
hdat3 <- hdat3 %>% 
  mutate(sh= case_when(past_pm==1 ~ parties_evts*100/group_tot)) %>% 
  select(prt_share_cat, sh) %>% 
  filter(complete.cases(.))
hdat3 <- t(hdat3[,2])
colnames(hdat3) <- c("low pp", "high pp")
print(hdat3)
table <- rbind(table, cbind("past_pm", hdat1, hdat2, hdat3))
rm(list=ls(pattern="hdat"))

#opp_party

hdat1.1 <- dat %>%
  filter(!is.na(civil_soc_cat) & !is.na(opp_party)) %>% 
  group_by(civil_soc_cat, opp_party) %>% 
  summarise_at(vars(parties_evts), funs(sum(., na.rm=TRUE)))
hdat1.2 <- dat %>%
  filter(!is.na(civil_soc_cat) & !is.na(opp_party)) %>% 
  group_by(civil_soc_cat) %>% 
  summarise(group_tot=sum(parties_evts, na.rm=TRUE))
hdat1 <- merge(hdat1.1, hdat1.2)
hdat1 <- hdat1 %>% 
  mutate(sh= case_when(opp_party==1 ~ parties_evts*100/group_tot)) %>% 
  select(civil_soc_cat, sh) %>% 
  filter(complete.cases(.))
hdat1 <- t(hdat1[,2])
colnames(hdat1) <- c("low civ", "high civ")
print(hdat1)
hdat2.1 <- dat %>%
  filter(!is.na(region) & !is.na(opp_party)) %>% 
  group_by(region, opp_party) %>% 
  summarise_at(vars(parties_evts), funs(sum(., na.rm=TRUE)))
hdat2.2 <- dat %>%
  filter(!is.na(region) & !is.na(opp_party)) %>% 
  group_by(region) %>% 
  summarise(group_tot=sum(parties_evts, na.rm=TRUE))
hdat2 <- merge(hdat2.1, hdat2.2)
hdat2 <- hdat2 %>% 
  mutate(sh= case_when(opp_party==1 ~ parties_evts*100/group_tot)) %>% 
  select(region, sh) %>% 
  filter(complete.cases(.))
hdat2 <- t(hdat2[,2])
colnames(hdat2) <- c("NWE", "SE", "EE")
print(hdat2)
hdat3.1 <- dat %>%
  filter(!is.na(prt_share_cat) & !is.na(opp_party)) %>% 
  group_by(prt_share_cat, opp_party) %>% 
  summarise_at(vars(parties_evts), funs(sum(., na.rm=TRUE)))
hdat3.2 <- dat %>%
  filter(!is.na(prt_share_cat) & !is.na(opp_party)) %>% 
  group_by(prt_share_cat) %>% 
  summarise(group_tot=sum(parties_evts, na.rm=TRUE))
hdat3 <- merge(hdat3.1, hdat3.2)
hdat3 <- hdat3 %>% 
  mutate(sh= case_when(opp_party==1 ~ parties_evts*100/group_tot)) %>% 
  select(prt_share_cat, sh) %>% 
  filter(complete.cases(.))
hdat3 <- t(hdat3[,2])
colnames(hdat3) <- c("low pp", "high pp")
print(hdat3)
table <- rbind(table, cbind("opp_party", hdat1, hdat2, hdat3))
rm(list=ls(pattern="hdat"))

# arrange table

colnames(table)[1] <- "name"
table <- as.data.frame(table)

table[,-1] <- sapply(table[,-1],as.character)
table[,-1] <- sapply(table[,-1],as.numeric)

table <- data.frame(table)

in_paper_r <- c("mainstream", "past_pm", "opp_party")

table <- subset(table, name %in% in_paper_r, select=c("name", "low.pp", "high.pp"))

table <- reshape2::melt(table, id.vars="name")

table$variable <- as.character(table$variable)

table$variable[table$variable=="low.pp"] <- "High Diff."
table$variable[table$variable=="high.pp"] <- "Low Diff."

table$variable <- factor(table$variable, levels =  c("Low Diff.", "High Diff."))

table$name <- as.character(table$name)
table$name[table$name=="opp_party"] <- "Opposition Party"
table$name[table$name=="past_pm"] <- "Previously PM Party"
table$name[table$name=="mainstream"] <- "Mainstream Party Family"

table$name <- factor(table$name, levels = c("Mainstream Party Family",
                              "Previously PM Party",
                              "Opposition Party"))

ggplot(table, aes(x=variable, y=value, fill=variable)) +
  geom_bar(stat="identity") +
  facet_wrap(~name) +
  # geom_hline(yintercept = 50, linetype="dashed") +
  xlab("") + ylab("Share in party protests (pp.)") +
  # scale_x_discrete(labels=c("high.pp" = "Low Diff.",
  #                           "low.pp" = "High Diff.")) +
  scale_fill_manual(values=c("#2a2b2d", "#7f8082")) +
  guides(fill=FALSE) +
  theme_bw()

ggsave(paste0(path, "\\f2_party_type_avg.eps"),
       plot=last_plot(), dpi=200, scale=0.6, width=8, height=5)

ggsave(paste0(path, "\\f2_party_type_avg.png"),
       plot=last_plot(), dpi=200, scale=0.6, width=8, height=5)

